﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
using MvcGlobalisationSupport;

namespace PortalCurriculo.MvcWebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
        //    const string defautlRouteUrl = "{controller}/{action}/{id}";
        //    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //    var defaultRouteValueDictionary = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        //    var defaultRoute = new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler());
        //    routes.Add("DefaultGlobalised", new GlobalisedRoute(defaultRoute.Url, defaultRoute.Defaults));
        //    routes.Add("Default", new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler()));
            const string defautlRouteUrl = "{controller}/{action}/{id}";

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional });
            routes.Add("DefaultGlobalised", new GlobalisedRoute(defautlRouteUrl, defaultRouteValueDictionary));
            routes.Add("Default", new Route(defautlRouteUrl, defaultRouteValueDictionary, new MvcRouteHandler()));
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        //Using <globalization culture="en-GB" uiCulture="auto:en-GB" /> in web.congfig under System.Web instead
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.UserLanguages == null)
            {
                CultureManager.SetCulture("pt-BR");
                return;
            }

            //Note everything hardcoded, this mechanism is to be replaced!
            if (Request.UserLanguages.Length == 0)
                return;

            string language = Request.UserLanguages[0];
            //if (language.Substring(0, 2).ToLower() == "ar")
            CultureManager.SetCulture(language);
            // Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("ar");
        }
    }
}